package com.irdstudio.apicenter.console.controller.module.Pluponform;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.module.ModuleEntity;
import com.irdstudio.apicenter.console.dm.module.ModuleService;
import com.irdstudio.apicenter.console.dm.util.FootPage;
import com.irdstudio.apicenter.console.dm.util.FootPageDao;
import com.irdstudio.apicenter.console.util.CurrentDateUtil;
import com.irdstudio.apicenter.frame.core.jdbc.JdbcBaseDAO;
import com.irdstudio.apicenter.frame.core.jdbc.tablemodel.ITableModelDAO;
import com.irdstudio.apicenter.frame.maker.dm.SModulePageBase;
import com.irdstudio.apicenter.frame.maker.dm.SModuleParams;
import com.irdstudio.apicenter.frame.maker.dm.SPageBase;
import com.irdstudio.apicenter.frame.maker.dm.SPageParamMain;
import com.irdstudio.apicenter.frame.maker.dm.STemplateParam;
import com.irdstudio.apicenter.frame.util.BeanUtils;

/**
 * 平台模块管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/plupmodule")
public class PlupmoduleAction extends MacmsBaseAction {
	@Autowired
	private ModuleService moduleService;

	/*
	 * 查询平台模块信息057156686510
	 */
	@RequestMapping(value = "modulelist")
	public ModelAndView moduleList(HttpServletRequest request) throws Exception {

		// 获取页码和条数service

		int row = 0;
		int size = 0;

		String id = "";
		String name = "";
		String update = "";
		String appid = "";
		if (request.getParameter("appid") != null) {
			appid = request.getParameter("appid");
		}
		if (request.getParameter("update") != null) {
			update = request.getParameter("update");
		}
		if (request.getParameter("pageNo") != null) {
			row = Integer.parseInt(request.getParameter("pageNo"));
		}
		if (request.getParameter("pageSize") != null) {
			size = Integer.parseInt(request.getParameter("pageSize"));
		}
		if (request.getParameter("moduleid") != null) {

			id = request.getParameter("moduleid");// 模块编码
		}
		if (request.getParameter("modulename") != null) {

			name = request.getParameter("modulename");// 模块编码
		}
		String sql = "";
		// 获取查询语句
		if (id.equals("") && !name.equals("")) {
			sql = "select module_id,module_name,module_index_page_id,last_modify_date from s_module_base where "
					+ "  module_name='"
					+ name
					+ "' order by last_modify_date desc";
		} else if (name.equals("") && !id.equals("")) {
			sql = "select module_id,module_name,module_index_page_id,last_modify_date from s_module_base where module_id= '"
					+ id + "' order by last_modify_date desc";
		} else if (!name.equals("") && !id.equals("")) {
			sql = "select module_id,module_name,module_index_page_id,last_modify_date from s_module_base where module_id= '"
					+ id
					+ "' and module_name='"
					+ name
					+ "' order by last_modify_date desc";

		} else {
			sql = "select module_id,module_name,module_index_page_id,last_modify_date from s_module_base order by last_modify_date desc";
		}
		// 获取分页信息
		FootPageDao fpd = new FootPageDao();
		FootPage fp = new FootPage();
		// 获取sql的表名及条件
		String[] str = sql.split("from");

		fp = fpd.querypage(str[1].toString(), row, size);

		// 获取分页的起始编号
		row = (fp.getPageNo() - 1) * fp.getPageSize();
		ITableModelDAO tableModelDao = (ITableModelDAO) BeanUtils
				.getBean("tableModelDAO");
		sql = tableModelDao.getNativePageSql(sql, row, fp.getPageSize());

		ModelAndView modelAndView = new ModelAndView();
		List<ModuleEntity> modulelist = moduleService.queryModule(sql);
		modelAndView.addObject("modulelist", modulelist);
		modelAndView.addObject("moduleid", id);
		modelAndView.addObject("modulename", name);
		modelAndView.addObject("appid", appid);
		modelAndView.addObject("page", fp);
		if ("1".equals(update)) {
			modelAndView.setViewName("appframe/moduleList");
		} else {
			modelAndView.setViewName("admin/moduleList");
		}
		return modelAndView;
	}

	/**
	 * 模块编辑
	 * 
	 * @param model
	 * @param id
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@RequestMapping(value = "edit")
	public String edit(Model model,
			@RequestParam(value = "module_id", required = true) String id)
			throws SQLException, ClassNotFoundException {
		model.addAttribute("id", id);
		model.addAttribute("viewState", 0);
		return "admin/moduleEdit";
	}

	/**
	 * 模块基本信息编辑
	 * 
	 * @param model
	 * @param id
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@RequestMapping(value = "baseInfoEdit")
	public String baseInfoEdit(Model model,
			@RequestParam(value = "module_id", required = true) String id)
			throws SQLException, ClassNotFoundException {

		return "admin/moduleBaseInfoEdit";
	}

	/**
	 * 模块页面列表
	 * 
	 * @param request
	 * @param id
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@RequestMapping(value = "modulePageList")
	public ModelAndView modulePageList(HttpServletRequest request,
			@RequestParam(value = "module_id", required = true) String id)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		// 跳转到模块设置里的模块页面组成的标识
		String setModule = "";
		if (request.getParameter("setModule") != null) {
			setModule = request.getParameter("setModule");
		}
		String viewState = request.getParameter("viewState");
		// 获取所有参数
		// 查询数据并放入到上下文中
		List<SModulePageBase> list = moduleService.queryPageBase(id);
		modelAndView.addObject("smpList", list);
		modelAndView.addObject("module_id", id);
		modelAndView.addObject("viewState", viewState);

		// String sql =
		// "select * from s_module_page a,s_page_base b where a.module_id=? and a.page_id=b.page_id";
		// JdbcBaseDAO baseDao = BeanUtils.getBean(JdbcBaseDAO.class);
		// super.context.addDataList("smpList", baseDao.findMapList(sql,
		// new Object[]{moduleId}));

		// 将数据上下文放到属性中
		if ("1".equals(setModule)) {
			modelAndView.setViewName("appframe/pageFileList");
		} else {
			modelAndView.setViewName("admin/modulePageList");
		}
		return modelAndView;
	}

	/**
	 * 模块参数列表
	 * 
	 * @param model
	 * @param id
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@RequestMapping(value = "moduleParamList")
	public ModelAndView moduleParamList(HttpServletRequest request,
			@RequestParam(value = "module_id", required = true) String id)
			throws Exception {
		// 查询数据并放入到上下文中
		String viewState = request.getParameter("viewState");
		List<SModuleParams> smlist = moduleService.queryModuleParam(id);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("smpList", smlist);
		modelAndView.addObject("module_id", id);
		modelAndView.addObject("viewState", viewState);
		modelAndView.setViewName("admin/moduleParamList");

		// String sql = "select * from s_module_param where module_id=?";
		// JdbcBaseDAO baseDao = BeanUtils.getBean(JdbcBaseDAO.class);
		// super.context.addDataList("smpList", baseDao.findMapList(sql,
		// new Object[]{moduleId}));

		// 将数据上下文放到属性中
		return modelAndView;
	}

	/*
	 * 查询模块详细信息 输出json
	 */
	@RequestMapping(value = "moduleDetail")
	public ModelAndView moduleDetail(HttpServletRequest request,
			@RequestParam(value = "id", required = true) String id)
			throws SQLException, ClassNotFoundException {
		// 查询数据并放入到上下文中
		ModelAndView modelAndView = new ModelAndView();
		// ModuleEntity moduleEntity = moduleService.queryModuleDetail(id);
		// List<SModuleParams> sp = (List<SModuleParams>)
		// moduleService.queryModuleParam(id);
		modelAndView.addObject("id", id);
		modelAndView.addObject("viewState", 1);
		modelAndView.setViewName("admin/moduleEdit");
		return modelAndView;
	}

	/*
	 * param_code[] 根据id跳转到模块制作界面
	 */
	@RequestMapping(value = "create")
	public ModelAndView create(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/moduleCreate");
		return modelAndView;
	}

	/*
	 * 根据id跳转到模块跟新界面
	 */
	@RequestMapping(value = "updateview")
	public ModelAndView updateview(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String id = request.getParameter("module_id");
		String viewState = request.getParameter("viewState");
		ModuleEntity moduleEntity = moduleService.queryModuleDetail(id);
		List<SModuleParams> sp = moduleService.queryModuleParam(id);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("moduleEntity", moduleEntity);
		modelAndView.addObject("modulelist", sp);
		modelAndView.addObject("viewState", viewState);
		modelAndView.setViewName("admin/moduleUpdate");
		return modelAndView;
	}

	/*
	 * 根据id跳转到模块跟新界面
	 */
	@RequestMapping(value = "modulepagelist")
	public ModelAndView modulepagelist(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String moduleId = request.getParameter("module_id");
		List<SModulePageBase> sp = moduleService.modulepagelist();
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("modulelist", sp);
		request.setAttribute("moduleid", moduleId);
		modelAndView.setViewName("admin/showModulePage");
		return modelAndView;
	}

	/**
	 * 根据id获取当前id是否存在
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "findid")
	public @ResponseBody
	ModelAndView findid(HttpServletRequest request) throws Exception {
		// 传入参数获取列表信息，将结果推送到界面
		String id = request.getParameter("appid");
		ModelAndView modelAndView = new ModelAndView();
		// int state= moduleService.findId(id);
		modelAndView.addObject("idflag", 0);
		return modelAndView;
	}

	/*
	 * 新增模块
	 */
	@RequestMapping(value = "addmodel")
	public @ResponseBody
	ModelAndView addModel(HttpServletRequest request, ModuleEntity meoduleEntity)
			throws SQLException, ClassNotFoundException {
		ModelAndView modelAndView = new ModelAndView();
		String sql = "insert into s_module_base(module_id,module_name,module_index_page_id,last_modify_date) values (?,?,?,?)";
		JdbcBaseDAO baseDao = BeanUtils.getBean(JdbcBaseDAO.class);
		int bl = baseDao.update(sql, meoduleEntity.getModule_id(),
				meoduleEntity.getModule_name(), meoduleEntity
						.getModule_index_page_id(), CurrentDateUtil
						.getTodayDate());
		modelAndView.addObject("addflag", bl);
		return modelAndView;
	}

	/*
	 * 更新模块
	 */
	@RequestMapping(value = "updatemodel")
	public @ResponseBody
	ModelAndView updatemodel(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String moduleid = (String) request.getParameter("strid");
		ModelAndView modelAndView = new ModelAndView();
		String str = (String) request.getParameter("v");
		String[] sp = str.split(";");
		List<SModuleParams> list = new ArrayList<SModuleParams>();
		SModuleParams smp = null;
		for (int i = 0; i < sp.length; i++) {
			if (sp[i] != null) {
				smp = new SModuleParams();
				String[] str2 = sp[i].split(",");
				smp.setModuleId(moduleid);
				smp.setParamCode(str2[0]);
				smp.setParamName(str2[1]);
				smp.setParamDefaultValue(str2[2]);
				smp.setParamRemarks(str2[3]);
				list.add(smp);

			}

		}
		int bl = moduleService.updatemodelparam(moduleid, list);
		modelAndView.addObject("flag", bl);
		return modelAndView;
	}

	/*
	 * 更新s_module_page表
	 */
	@RequestMapping(value = "updateModulePage")
	public @ResponseBody
	ModelAndView updateModulePage(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String moduleid = (String) request.getParameter("strid");
		ModelAndView modelAndView = new ModelAndView();
		String str = (String) request.getParameter("v");
		String[] sp = str.split(";");
		List<SModulePageBase> list = new ArrayList<SModulePageBase>();
		SModulePageBase smp = null;
		for (int i = 0; i < sp.length; i++) {
			if (sp[i] != null) {
				smp = new SModulePageBase();
				String[] str2 = sp[i].split(",");
				smp.setPageId(str2[0]);
				list.add(smp);
			}

		}
		int bl = moduleService.updateModulePage(moduleid, list);
		modelAndView.addObject("flag", bl);
		return modelAndView;
	}

	/*
	 * 删除模块
	 */
	@RequestMapping(value = "deletemodel")
	public @ResponseBody
	ModelAndView deletemodel(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String id = (String) request.getParameter("id");
		ModelAndView modelAndView = new ModelAndView();
		int bl = moduleService.deletemodel(id);
		modelAndView.addObject("flag", bl);
		return modelAndView;
	}

	/*
	 * 删除模块
	 */
	@RequestMapping(value = "updatemodelbase")
	public @ResponseBody
	ModelAndView updatemodelbase(HttpServletRequest request,
			ModuleEntity meoduleEntity) throws SQLException,
			ClassNotFoundException {
		String id = (String) request.getParameter("id");
		ModelAndView modelAndView = new ModelAndView();
		JdbcBaseDAO baseDao = BeanUtils.getBean(JdbcBaseDAO.class);
		int bl = baseDao
				.update(
						"update s_module_base set module_name=?,module_index_page_id=?,long_desc=?,last_modify_date=? where module_id=?",
						meoduleEntity.getModule_name(), meoduleEntity
								.getModule_index_page_id(), meoduleEntity
								.getLong_desc(),
						CurrentDateUtil.getTodayDate(), meoduleEntity
								.getModule_id());
		modelAndView.addObject("flag", bl);
		return modelAndView;
	}
	
	/**
	 * 添加页面
	 * @param moduleId,pageId
	 * @param request
	 * @param response
	 * @throws SQLException
	 */
	@RequestMapping(value = "addPage")
	public void addPage(HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		String moduleId = request.getParameter("moduleid");
		String pageId = request.getParameter("pageid");
		System.out.println(moduleId+"-----"+pageId);
		int flag = -1;
		JdbcBaseDAO baseDao = BeanUtils.getBean(JdbcBaseDAO.class);
		String sql1 = "insert into s_module_page(module_id,page_id) values(?,?)";
		baseDao.insert(sql1, moduleId, pageId);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}

	}
	/**
	 * 移除页面
	 * @param moduleId,pageId
	 * @param request
	 * @param response
	 * @throws SQLException
	 */
	@RequestMapping(value = "delPage")
	public void delPage(HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		String moduleId = request.getParameter("moduleid");
		String pageId = request.getParameter("pageid");
		System.out.println(moduleId+"-----"+pageId);
		int flag = -1;
		JdbcBaseDAO baseDao = BeanUtils.getBean(JdbcBaseDAO.class);
		String sql = "delete from s_module_page where module_id=? and page_id=?";
		flag = baseDao.delete(sql, moduleId, pageId);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}

	}
}
